package group.rober.dataform.handler.impl;

import group.rober.dataform.handler.DataListHandler;
import group.rober.dataform.model.DataForm;
import group.rober.saber.jdbc.query.PaginationQueryResult;
import group.rober.saber.jdbc.query.executor.PaginationQueryExecutor;
import group.rober.saber.kit.ValidateKit;
import group.rober.saber.lang.DataBox;
import group.rober.saber.lang.ValueObject;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

/**
 * 默认实现JavaBean列表数据的基本处理<br/>
 * JavaBean的操作,该类必需为抽象类,因为它不知道数据类到底是什么,只能由实现类
 * Created by tisir<yangsong158@qq.com> on 2017-05-29
 */
public abstract class BeanListHandler<T> implements DataListHandler<T> {
    @Autowired
    protected PaginationQueryExecutor queryExecutor;

    public PaginationQueryExecutor getQueryExecutor() {
        return queryExecutor;
    }

    public void setQueryExecutor(PaginationQueryExecutor queryExecutor) {
        this.queryExecutor = queryExecutor;
    }


    public void beforeQuery(DataForm dataForm, Map<String, Object> queryParameters, Map<String, ValueObject> filterParameters, Map<String,String> sortMap, int pageSize, int pageIndex) {

    }

    protected Class<?> getBeanClass(){
        Type t = getGenericImplClass().getGenericSuperclass();
        System.out.println(getClass());
        Type[] params = null;
        try{
            params = ((ParameterizedType) t).getActualTypeArguments();
        }catch(ClassCastException e){
            throw new RuntimeException("generic parameter error,at class:"+getClass().getName(),e);
        }
        ValidateKit.notEmpty(params,"class %s does not have generic support",t);
        Type genericType = params[0];
        ValidateKit.notNull(genericType,"class %s does not have generic parameter does not exist");
        Class<T> cls = (Class<T>) genericType;
        return cls;
//        System.out.println("");
//        System.out.println("起点Class:" + t);
//        System.out.println("泛型Class:" + genericType);
    }

    public PaginationQueryResult<T> query(DataForm dataForm, Map<String, Object> queryParameters, Map<String, ValueObject> filterParameters,Map<String,String> sortMap, int pageSize, int pageIndex) {
        this.beforeQuery(dataForm,queryParameters,filterParameters,sortMap,pageSize,pageIndex);

        FunctionInHandler.ListQuery handlerListQuery = new FunctionInHandler.ListQuery<DataBox>(queryExecutor,dataForm);
        handlerListQuery.setQueryParameters(queryParameters);
        handlerListQuery.setFilterParameters(filterParameters);
        handlerListQuery.setPageSize(pageSize);
        handlerListQuery.setPageIndex(pageIndex);
        PaginationQueryResult<T> result = handlerListQuery.queryPaginationList(getBeanClass());

        this.afterQuery(dataForm,queryParameters,filterParameters,sortMap,pageSize,pageIndex,result);

        return result;
    }

    public void afterQuery(DataForm dataForm, Map<String, Object> queryParameters, Map<String, ValueObject> filterParameters,Map<String,String> sortMap, int pageSize, int pageIndex, PaginationQueryResult<T> result) {

    }

    public List<T> save(DataForm dataForm, List<T> dataList) {
        return null;
    }

    public void beforeInsert(DataForm dataForm, List<T> dataList) {

    }

    public void afterInsert(DataForm dataForm, List<T> dataList) {

    }

    public void beforeUpdate(DataForm dataForm, List<T> dataList) {

    }

    public void afterUpdate(DataForm dataForm, List<T> dataList) {

    }

    public Integer delete(DataForm dataForm, List<T> dataList) {
        return null;
    }

    public void beforeDelete(DataForm dataForm, List<T> dataList) {

    }

    public void afterDelete(DataForm dataForm, List<T> dataList) {

    }

    /**
     * 本类下,对泛型数据有实现模型的实现类节点
     * @return
     */
    protected Class<?> getGenericImplClass(){
        return getClass();
    }
}
